package logic

import (
	"context"
	"fmt"
	"user-profile/models"

	"user-profile/user_rpc/internal/svc"
	"user-profile/user_rpc/user_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FindLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FindLogic {
	return &FindLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FindLogic) Find(in *user_rpc.FindRequest) (*user_rpc.FindResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if models.DB.Where("id = ?", in.Id).Limit(1).Find(&user).Error != nil {
		return &user_rpc.FindResponse{}, fmt.Errorf("服务器繁忙")
	}
	if user.ID == 0 {
		return &user_rpc.FindResponse{}, fmt.Errorf("账号不存在")
	}
	return &user_rpc.FindResponse{
		UserName:    user.UserName,
		UserAccount: user.UserAccount,
		UserImage:   user.UserImage,
	}, nil
}
